//
//  HHUserCollectionViewCell.m
//  HHChatKit
//
//  Created by Henry on 2021/8/19.
//

#import "HHUserCollectionViewCell.h"
#import "HHSelectUserModel.h"
#import "UIImageView+HHChat.h"
#import "HHResources.h"

@interface HHUserCollectionViewCell ()

@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UIImageView *iconView;

@end

@implementation HHUserCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.iconView];
}

- (void)setupTheme:(HHChatDetailTheme *)theme {
    self.iconView.frame = CGRectMake(0, 0, theme.imageHeight, theme.imageHeight);
    self.nameLabel.frame = CGRectMake(0, theme.imageHeight, self.frame.size.width, self.frame.size.height-theme.imageHeight);
    
    self.nameLabel.font = theme.nameFont;
    self.nameLabel.textColor = theme.nameColor;
}

- (void)setModel:(HHSelectUserModel *)model {
    _model = model;
    self.nameLabel.text = model.name ?: @"";
    if (model.portraitImage) {
        self.iconView.image = model.portraitImage;
    } else {
        [self.iconView hh_setAvatarWithUrl:model.portrait ID:model.userId name:model.name];
    }
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];        
        _nameLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _nameLabel;
}

- (UIImageView *)iconView {
    if (!_iconView) {
        _iconView = [[UIImageView alloc] init];
        _iconView.contentMode = UIViewContentModeScaleAspectFit;
        [_iconView hc_setCornerRadius];
    }
    return _iconView;
}

@end
